#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
调用测试API获取加密数据
"""

import requests
import json
import sys
import io

# 修复Windows编码问题
if sys.platform == 'win32':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
    sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')

API_URL = "http://120.27.155.222:9999/api/message/send-test"

def call_api():
    print("=" * 80)
    print("📡 调用数美SDK测试API")
    print("=" * 80)
    print(f"URL: {API_URL}\n")

    try:
        print("发送请求...")
        response = requests.get(API_URL, timeout=10)
        
        print(f"状态码: {response.status_code}\n")
        
        if response.status_code == 200:
            data = response.json()
            
            print("=" * 80)
            print("✅ API响应成功")
            print("=" * 80)
            print(json.dumps(data, indent=2, ensure_ascii=False))
            print("=" * 80)
            
            # 保存到文件
            with open('api_response.json', 'w', encoding='utf-8') as f:
                json.dump(data, f, indent=2, ensure_ascii=False)
            
            print("\n✅ 响应已保存到 api_response.json")
            
            # 提取关键字段
            if 'ep' in data:
                print(f"\n🔑 EP字段: {data['ep'][:50]}...")
            if 'data' in data:
                print(f"📦 data字段: {data['data'][:50]}...")
            
            return data
        else:
            print(f"❌ 请求失败: {response.status_code}")
            print(response.text)
            return None
            
    except Exception as e:
        print(f"❌ 错误: {e}")
        import traceback
        traceback.print_exc()
        return None

if __name__ == "__main__":
    call_api()

